/**
 * Copyright (c) 2020 铭软科技(mingsoft.net)
 * 本软件及相关文档文件（以下简称“软件”）的版权归 铭软科技 所有
 * 遵循铭软科技《保密协议》
 */
package net.mingsoft.mwebsite.handler;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import net.mingsoft.basic.entity.AppEntity;
import net.mingsoft.basic.util.BasicUtil;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class AppHandler implements TenantLineHandler {
    @Value("${ms.mwebsite.tables:cms_category,cms_content,mdiy_dict,mdiy_model,mdiy_page,people,people_address,role,mpay_log,mpay_pay,wx_mini_app,wx_weixin}")
    private String[] tables;
    @Override
    public Expression getTenantId() {
        AppEntity appEntity = BasicUtil.getWebsiteApp();
        int appId = 0;
        if(appEntity!=null){
            appId = appEntity.getAppId();
        }
        return new LongValue(appId);
    }

    @Override
    public String getTenantIdColumn() {
        return "app_id";
    }

    @Override
    public boolean ignoreTable(String tableName) {
        return ArrayUtil.isEmpty(tables)|| !ArrayUtil.containsIgnoreCase(tables,tableName);
    }
}
